package com.autumn.platform.auth.core.authc.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.autumn.platform.auth.core.authc.IAuthenticationToken;
import com.autumn.platform.auth.core.authc.IAuthenticator;
import com.autumn.platform.core.context.response.IResponse;

public class AuthenticatorComposite implements IAuthenticator {

    @Autowired(required = false)
    private List<IAuthenticator> authenticators;

    @Override
    public IResponse login(IAuthenticationToken authenticationToken) throws Exception {
        IResponse first = null;
        List<IAuthenticator> authenticators = getAuthenticators();
        if (null != authenticators && !authenticators.isEmpty()) {
            for (IAuthenticator authenticator : authenticators) {
                IResponse response = authenticator.login(authenticationToken);
                if (null != response) {
                    if (response.isSuccess()) {
                        return response;
                    } else if (null == first) {//不成功，记录第一个不成功的状态
                        first = response;
                    }
                }
            }
        }
        return first;
    }

    public List<IAuthenticator> getAuthenticators() {
        return authenticators;
    }

    public void setAuthenticators(List<IAuthenticator> authenticators) {
        this.authenticators = authenticators;
    }
}
